package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 晓帆
 * @version 1.0
 **/
@Service
@Slf4j
public class IHomeServiceImpl implements IHomeService {
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter converter;

    /**
     * 商品主页
     * @return HomePageResponse
     */
    @Override
    public HomePageResponse homepage() {
        HomePageResponse homePageResponse = new HomePageResponse();
        try {
            List<Panel> panels = panelMapper.selectAll();
            Set<PanelDto> panels1 = new HashSet<>();
            for (Panel panel : panels) {
                PanelDto panelDto = new PanelDto();
                panelDto.setId(panel.getId());
                panelDto.setLimitNum(panel.getLimitNum());
                panelDto.setName(panel.getName());
                panelDto.setPosition(panel.getPosition());
                panelDto.setRemark(panel.getRemark());
                panelDto.setSortOrder(panel.getSortOrder());
                panelDto.setStatus(panel.getStatus());
                panelDto.setType(panel.getType());
                Integer id = panel.getId();
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(id);
                List<PanelContentItemDto> panelContentItemDtos = converter.panelContentItem2Dto(panelContentItems);
                panelDto.setPanelContentItems(panelContentItemDtos);
                panels1.add(panelDto);
            }
            homePageResponse.setPanelContentItemDtos(panels1);
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return homePageResponse;
    }
}
